import Vue from 'vue'
import VueRouter from 'vue-router'
import store from '@/store';
import { $bus } from '../main'
import { clearSessionStorage } from '../utils/utils';
Vue.use(VueRouter)
const routes = [
  {
    path: '/',
    name: 'index',
    component: () => import('@/views/Index'),
    redirect: '/',
    children: [
      {
        path: '/',
        component: () => import('@/components/main/Index')
      },
      {
        path: 'detail/:id',
        name: 'detail',
        component: () => import('@/components/article-detail/Index')
      },
      {
        path: 'user',
        name: 'user',
        component: () => import('@/components/user/UserDetail'),
        redirect: `/user/dynic`,
        children: [
          {
            path: 'dynic', // 表示可有可无
            name: 'dynic',
            component: () => import("@/components/user/userDetail/userDynic/navMenu/Dynic")
          },
          {
            path: 'user-article',
            name: 'user-article',
            component: () => import("@/components/user/userDetail/userDynic/navMenu/Article")
          },
          {
            path: 'boil',
            name: 'boil',
            component: () => import("@/components/user/userDetail/userDynic/navMenu/Boil")
          },
          {
            path: 'agree',
            name: 'agree',
            component: () => import("@/components/user/userDetail/userDynic/navMenu/Agree")
          },
          {
            path: 'userFocus',
            name: 'userFocus',
            component: () => import("@/components/user/userDetail/userDynic/navMenu/UserFocus")
          },
        ]
      },
      {
        path: 'changeUserMsg',
        name: 'changeUserMsg',
        component: () => import('@/views/ChangeUserMsg')
      },
      {
        path: 'search',
        name: 'search',
        component: () => import('@/views/Search')
      }
    ]
  },
  {
    path: '/write',
    name: 'write',
    component: () => import('@/views/Write')
  }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})



router.beforeEach((to, from, next) => {
  // 刷新登录
  store.dispatch('refresh');
  $bus.$emit('changeIsShow', to.path !== '/' ? false : true);

  // 如果当前路由不在user, 清除掉session中的keyPath
  if (!to.path.includes('user')) {
    clearSessionStorage('keyPath');
  }
  next();
})


export default router
